<?php
/**
 * PsiFramework
 *
 * Php Simple Framework
 *
 * PHP version 5.3
 *
 * @category  PsiFramework.Plugins.CMS.Classes.Actions
 * @package   PsiFramework.Plugins.CMS.Classes.Actions
 * @author    Remi San <remi.san@gmail.com>
 * @copyright 2011 Remi San
 * @license   http://creativecommons.org/licenses/by/3.0/  Creative Commons CC BY 3.0
 * @version   SVN: <svn_id>
 * @link      http://www.psiframework.com/phpdoc/classes/db_\PsiFramework\Plugins.CMS.Classes.Actions.ListAction.html
 */

namespace PsiFramework\Plugins\CMS\Classes\Actions;

import("Classes.Actions.AbstractAction");
import("Classes.Factory.DAOFactory");
import("Classes.Util.Database.Filters.Paginator");
import("Classes.Util.Database.Query.SqlColumn");
import("Classes.Util.Database.Query.SqlOrder");
import("Classes.Util.Manager.ApplicationManager");

use \PsiFramework\Classes\Actions\AbstractAction;
use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Classes\Util\Database\Filters\Paginator;
use \PsiFramework\Classes\Util\Database\Query\SqlColumn;
use \PsiFramework\Classes\Util\Database\Query\SqlOrder;
use \PsiFramework\Classes\Util\Manager\ApplicationManager;
use \PsiFramework\Classes\Util\UI\TemplateManager;

/**
 * Default list action
 * 
 * This is the default class for the list action.
 *
 * @category PsiFramework.Plugins.CMS.Classes.Actions
 * @package  PsiFramework.Plugins.CMS.Classes.Actions
 * @author   Remi San <remi.san@gmail.com>
 * @license  http://creativecommons.org/licenses/by/3.0/  Creative Commons CC BY 3.0
 * @link     http://www.psiframework.com/phpdoc/classes/db_\PsiFramework\Plugins.CMS.Classes.Actions.ListAction.html
 */
class ListAction extends AbstractAction
{

    /**
     * 
     * @param unknown_type $nbPage
     * @param unknown_type $nbResult
     * @param unknown_type $orderAscDesc
     */
    public function getPaginator(/*int*/ $nbPage=1, /*int*/ $nbResult=25, /*string*/ $orderAscDesc='ASC')
    {
        
        $sens = $this->_request->getParameter('sens');
        if ($sens == null) {
            $sens = $orderAscDesc ;
        }
        $orderColumn = $this->_request->getParameter('orderColumn');
        $order = null ;
        
        $applicationManager = ApplicationManager::getInstance();
        $application = $applicationManager->getApplication();
        $objectParameters = $application->getObjectParameter($this->_objectType);
        
        if (isNullOrEmptyString($orderColumn)) {
            $orderColumn = 'id' ;
            $pk = $this->retrieveDao()->getObjectParameters()->getPrimaryKey();
            if (!is_array($pk)) {
                $orderColumn = $pk->name;
            } else if (count($pk)==1) {
                $orderColumn = $pk[0]->name;
            }
        }
        $column = $objectParameters->getParameter($orderColumn);
        
        if (!isNullOrEmptyString($column)) {
            $sqlColumn = new SqlColumn($column->column);
            $order = new SqlOrder($sqlColumn, $sens);
        }
        
        $page = $this->_request->getParameter('page');
        $nbResults = $this->_request->getParameter('nbResults');
        
        $paginator = null ;
        if (!is_null($order) && $nbResult!=0) {
            $paginator = new Paginator();
            $paginator->addOrder($order);
        
            if (isNullOrEmptyString($page)) {
                $page = $nbPage ;
            }
            if (isNullOrEmptyString($nbResults)) {
                $nbResults = $nbResult ;
            }
        
            $paginator->setNbResults($nbResults);
            $paginator->setPage($page);
        }
        
        $this->_response->setAttribute("columns", $objectParameters->getParameters());
        $this->_response->setAttribute("sens", $sens);
        $this->_response->setAttribute("orderColumn", $orderColumn);
        $this->_response->setAttribute("page", $page);
        $this->_response->setAttribute("nbResults", $nbResults);
        
        return $paginator;
    }
    
    public function retrieveDao()
    {
        $dao = DAOFactory::getDAO($this->_objectType);
        if (is_null($dao)) {
            throw new \Exception(
                "Impossible to retrieve DAO for " . $this->_objectType
            );
        }
        
        return $dao;
    }
    
    public function retrieveList(/*int*/ $nbPage=1, /*int*/ $nbResult=25, /*string*/ $orderAscDesc='ASC')
    {
        
        $dao = $this->retrieveDao();
        
        $totalResults = $dao->count();
        $this->_response->setAttribute("totalResults", $totalResults);
        
        $list = $dao->retrieveAll(null, $this->getPaginator($nbPage, $nbResult, $orderAscDesc));
        $this->_response->setAttribute("list", $list);
        $this->_response->setAttribute("searchService", $dao->getSearchService());
    }
    
    /**
     * The main function of the method which is called every time
     * an action is created by the front controller.
     * 
     * @see    AbstractAction::execute()
     * @return void
     */
    public function execute(/*string*/ $successVar='success', /*int*/ $nbPage=1, /*int*/ $nbResult=25, /*string*/ $orderAscDesc='ASC')
    {
        parent::execute();
        
        $moreParams = array();
        //TODO gérer les params supplémentaires
        
        $this->retrieveList($nbPage, $nbResult, $orderAscDesc);
        
        $this->_response->setAttribute("moreParams", $moreParams);

        $templateManager = TemplateManager::getInstance();
        $templateManager->addRemoteScript(
    		'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'
        );
        $templateManager->addRemoteScript(
    		'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/jquery-ui.min.js'
        );
        
        if (!isNullOrEmptyString($successVar)) {
            $this->findForward($successVar);
        }
    }

}
